package svc

import (
	"github.com/zeromicro/go-queue/kq"
	"mall/service/attachment/rpc/attachmentclient"
	"mall/service/backend/common/gorseclient"
	"mall/service/blog/rpc/blogclient"
	"mall/service/forum/rpc/forumclient"
	"mall/service/mqueue/job/internal/config"
	"mall/service/order/rpc/orderclient"
	"mall/service/product/rpc/productclient"
	"mall/service/promotion/rpc/promotionclient"
	"mall/service/recommended/rpc/recommendedclient"
	"mall/service/user/rpc/userclient"

	"github.com/hibiken/asynq"
	"github.com/silenceper/wechat/v2/miniprogram"
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
	Config config.Config

	AsynqServer *asynq.Server
	MiniProgram *miniprogram.MiniProgram
	BizRedis    *redis.Redis
	GorseClient gorseclient.GorseClient

	KqDeferredUnFreezePointCouponClient *kq.Pusher

	AttachmentRpc  attachmentclient.Attachment
	BlogRpc        blogclient.Blog
	ForumRpc       forumclient.Forum
	OrderRpc       orderclient.Order
	ProductRpc     productclient.Product
	PromotionRpc   promotionclient.Promotion
	UserRpc        userclient.User
	RecommendedRpc recommendedclient.Recommended
}

func NewServiceContext(c config.Config) *ServiceContext {
	return &ServiceContext{
		Config: c,

		AsynqServer: newAsynqServer(c),
		MiniProgram: newMiniprogramClient(c),
		GorseClient: gorseclient.NewGorseClient(),

		KqDeferredUnFreezePointCouponClient: kq.NewPusher(
			c.KqUnFreezePointCouponConf.Brokers,
			c.KqUnFreezePointCouponConf.Topic,
		),

		AttachmentRpc:  attachmentclient.NewAttachment(zrpc.MustNewClient(c.AttachmentRpc)),
		BlogRpc:        blogclient.NewBlog(zrpc.MustNewClient(c.BlogRpc)),
		ForumRpc:       forumclient.NewForum(zrpc.MustNewClient(c.ForumRpc)),
		OrderRpc:       orderclient.NewOrder(zrpc.MustNewClient(c.OrderRpc)),
		ProductRpc:     productclient.NewProduct(zrpc.MustNewClient(c.ProductRpc)),
		PromotionRpc:   promotionclient.NewPromotion(zrpc.MustNewClient(c.PromotionRpc)),
		UserRpc:        userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),
		RecommendedRpc: recommendedclient.NewRecommended(zrpc.MustNewClient(c.RecommendedRpc)),
	}
}
